<?php

namespace Api\Model;
use Common\Model\TagModel as BashModel;
use Api\Model\TagLinkModel;
use Api\Model\LogInfoModel;
use Api\Model\FileModel;
use Api\Model\ProjectModel;

/**
 * 标签表，包含有所有的标签
 * 
 * @author xiaoyutab<xiaoyutab@qq.com>
 * @version v1.0.0
 * @copyright (c) 2017, xiaoyutab
 * @adtime 2018-04-23 17:23:09
 */
class TagModel extends BashModel{
    protected $link = null;
    protected $log = null;
    protected $file = null;
    protected $project = null;


    /**
     * 获取文件链接表
     * 
     * @return type
     */
    public function get_link(){
        if($this->link){
            return $this->link;
        }
        $this->link = new TagLinkModel();
        return $this->link;
    }
    
    public function get_project(){
        if($this->project){
            return $this->project;
        }
        $this->project = new ProjectModel();
        return $this->project;
    }

    /**
     * 获取文件表连接
     * 
     * @return type
     */
    public function get_file(){
        if($this->file){
            return $this->file;
        }
        $this->file = new FileModel();
        return $this->file;
    }

    /**
     * 获取日志详情表
     * @return type
     */
    public function get_loginfo(){
        if($this->log){
            return $this->log;
        }
        $this->log = new LogInfoModel();
        return $this->log;
    }

    /**
     * 获取表编号
     * 
     * @param type $tab
     * @return type
     */
    public function get_table_id($tab = ''){
        return $this->get_loginfo()->get_id($tab);
    }
    
    /**
     * 获取首页的案例列表
     * 
     * @param type $id
     * @return boolean
     */
    public function get_anli_list($id = false){
        if(!$id){
            return false;
        }
        $lists = $this->get_link()->where(array('target_id'=>$id,'target_name'=> $this->get_table_id('tag')))->getList(0,999);
        $return_data = array();
        foreach($lists as $v){
            $temp = $this->get_info($v['file_id']);
            $file = $this->get_file()->get_img_type('tag',$v['file_id'],FALSE,'file_path');
            // 获取案例所关联的产品
            $project = $this->get_link()->where(array('target_name'=> $this->get_table_id('project'),'file_id'=>$v['file_id']))->getList(0,999);
            $project_data = array();
            foreach($project as $k => $v){
                $temp = $this->get_project()->get_info($v['target_id']);
                $project_data[] = array(
                    'name' => $temp['name'],
                    'price' => $temp['price'],
                    'wl_no' => $temp['wl_no'],
                    'desc' => $temp['content'],
                    'file' => $this->get_file()->get_img_type('project',$temp['id'],FALSE,'file_path'),
                );
            }
            $return_data[] = array(
                'id' => $v['file_id'],
                'name' => $temp['name'],
                'desc' => $temp['desc'],
                'file' => $file,
                'project' => $project_data,
            );
        }
        return $return_data;
    }
}